using System;
using System.IO;
using System.Xml.Serialization;
using System.Drawing;
using System.Runtime.InteropServices;

namespace WPChanger
{
	/// <summary>
	/// Summary description for WPManager.
	/// </summary>
	public class WPManager
	{
		private const int SPI_SETDESKWALLPAPER = 20;
		private const int SPIF_SENDCHANGE = 0x2;
		private const string BMP = ".bmp";

		private WPProvider _WPProvider = null;
		private Settings _Settings = null;
		private System.Windows.Forms.Timer _Timer = null;
		private string _WPPath = "";
		private string _CurrentWP = "";
		
		public WPManager(System.Windows.Forms.Timer aTimer, string settingsFileName, string currentWPPath)
		{
			_WPPath = currentWPPath;

			_Settings = SettingsManager.loadSettings(settingsFileName);
			_WPProvider = new WPProvider(_Settings);
			_Timer = aTimer;
		}

		public WPManager(System.Windows.Forms.Timer aTimer, string currentWPPath)
		{
			_WPPath = currentWPPath;

			//_Settings = SettingsManager.loadSettings(settingsFileName);
			//_WPProvider = new WPProvider(_Settings);
			_Timer = aTimer;
		}

		public WPProvider Provider
		{
			get
			{
				return _WPProvider;
			}
			set
			{
				_WPProvider = value;
			}
		}
		public Settings mySettings
		{
			get
			{
				return _Settings;
			}
			set
			{
				_Settings = value;
			}
		}

		//***********************************************
		//WP OPERATION
		//***********************************************
		public bool delete()
		{
			if (File.Exists(_CurrentWP))
			{
				File.Delete(_CurrentWP);
				_CurrentWP = "";
				return true;
			}
			else
			{
				return false;
			}
		}
		public bool copyTo(string destinationFile, bool overWrite)
		{
			if (File.Exists(_CurrentWP))
			{	
				File.Copy(_CurrentWP,destinationFile,overWrite);
				return true;
			}
			else
			{
				return false;
			}
		}
		public bool moveTo(string destinationFile, bool overWrite)
		{
			if (File.Exists(_CurrentWP))
			{
				if (File.Exists(destinationFile))
				{
					if (overWrite == true)
					{
						File.Delete(destinationFile);
					}
					else
					{
						return false;
					}
				}
				File.Move(_CurrentWP, destinationFile);
				return true;
			}
			else
			{
				return false;
			}
		}

		//***********************************************
		//TIMER
		//***********************************************
		public void enableTimer()
		{
			int interval = _Settings.TimerInterval;
			if (interval > 0)
			{ 
				_Timer.Interval = interval * 1000;
				_Timer.Tick+=new EventHandler(_Timer_Tick);
				_Timer.Enabled = true;	
				_Timer.Start();
			}
		}
		public void disableTimer()
		{
			_Timer.Tick -= null;
			_Timer.Enabled = false;
			_Timer.Stop();
		}

		private void _Timer_Tick(object sender, EventArgs e)
		{
			this.SetWP(_WPProvider.getRandomWP());
		}
		
		/// <summary>
		/// Set the new BMP in the background
		/// </summary>
		public void SetWP(string fileName)
		{
			if (File.Exists(fileName))
			{
				_CurrentWP = fileName;
				Bitmap theImage = new Bitmap(fileName);
				if (Path.GetExtension(fileName).ToLower() != BMP)
				{
					theImage.Save (_WPPath, System.Drawing.Imaging.ImageFormat.Bmp);
				}
				else
				{
					theImage.Save(_WPPath);
				}
				theImage.Dispose();
				int nResult =  WinAPI.SystemParametersInfo(SPI_SETDESKWALLPAPER,1,_WPPath,SPIF_SENDCHANGE);
			}
			/*else
			{
				throw new FileNotFoundException("File Not Found!", fileName);
			}*/
		}
	}

	public class WinAPI
	{
		[DllImport("user32.dll", CharSet=CharSet.Auto)]
		public static  extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;
		public const int SPI_SETDESKWALLPAPER = 20;
		public const int SPIF_SENDCHANGE = 0x2;

	}  
}
